package com.badboy.mycarbon.system.common;

import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
 * 通用返回结果类
 */
@Data
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 消息
     */
    private String msg;

    /**
     * 时间戳
     */
    private String time;

    private Result() {}

    private Result(Integer code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        this.time = new Date().toString();
    }

    /**
     * 成功返回结果
     */
    public static <T> Result<T> success() {
        return new Result<>(200, null, "操作成功");
    }

    /**
     * 成功返回结果
     */
    public static <T> Result<T> success(T data) {
        return new Result<>(200, data, "操作成功");
    }

    /**
     * 成功返回结果
     */
    public static <T> Result<T> success(T data, String msg) {
        return new Result<>(200, data, msg);
    }

    /**
     * 失败返回结果
     */
    public static <T> Result<T> error(String msg) {
        return new Result<>(500, null, msg);
    }

    /**
     * 失败返回结果
     */
    public static <T> Result<T> error(Integer code, String msg) {
        return new Result<>(code, null, msg);
    }
}